<template>
  <div class="displist-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button :icon="Search" plain type="primary" @click="openQueryDialog">
          查询
        </el-button>
        <el-button
          :disabled="!currentDispsRef"
          :icon="Open"
          plain
          type="primary"
          @click="fanJungong"
        >
          打开
        </el-button>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-table
      :cell-style="{ padding: '6px 6px' }"
      :data="dispH"
      highlight-current-row
      @current-change="handleCurrentChange"
    >
      <el-table-column label="序号" type="index" width="80" />
      <el-table-column label="订单号" prop="scddh" width="150" />
      <el-table-column label="存货编码" prop="invcode" width="180" />
      <el-table-column label="存货名称" prop="invname" />
      <el-table-column label="规格型号" prop="invspec" />
      <el-table-column label="订单数量" prop="jhwgsl" width="150" />
      <el-table-column label="工作中心" prop="wcName" width="150" />
      <el-table-column label="工序" prop="opName" width="150" />
      <el-table-column label="创建人" prop="createUser" width="150" />
      <el-table-column label="创建时间" prop="createTime" width="220" />
    </el-table>
    <el-pagination
      background
      :current-page="paginationParams.pageNum"
      :layout="layout"
      :page-size="paginationParams.pageSize"
      :total="paginationParams.total"
      @current-change="handlePaginationCurrentChange"
      @size-change="handlePaginationSizeChange"
    />
    <div style="height: 28px"></div>
    <el-table
      :cell-style="{ padding: '6px 6px' }"
      :data="disps"
      highlight-current-row
      @current-change="handleCurrentChange2"
    >
      <el-table-column label="序号" type="index" width="80" />
      <el-table-column label="派工单号" prop="dispatchCode" width="150" />
      <el-table-column label="设备" prop="equipName" width="220" />
      <el-table-column label="班次" prop="banciDate" />
      <el-table-column label="班次" prop="banciName" />
      <el-table-column label="派工数量" width="150">
        <template #default="scope">
          <span style="font-size: 16px; font-weight: 600; color: #5314bd">
            {{ scope.row.dispatchQty }}&nbsp;{{ scope.row.measName }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="竣工" width="120">
        <template #default="scope">
          <el-icon v-if="scope.row.state === 0" color="#30DF80" size="20">
            <SemiSelect />
          </el-icon>
          <el-icon v-else color="#DF3730" size="20">
            <Select />
          </el-icon>
        </template>
      </el-table-column>
      <el-table-column label="报工数量" prop="reportQty" />
      <el-table-column label="合格数量" prop="qualifiedQty" />
      <el-table-column label="报检数量" prop="inspecQty" />
      <el-table-column label="报废数量" prop="scrapQty" />
      <el-table-column label="模具编码" prop="moldCode" />
      <el-table-column label="模具名称" prop="moldName" />
      <el-table-column label="模具型号" prop="moldSpec" />
      <el-table-column label="腔型编码" prop="cavityCode" />
      <el-table-column label="腔型名称" prop="cavityName" />
    </el-table>
    <queryDispDialog
      ref="queryDispDialogRef"
      @success-callback="queryCallback"
    />
  </div>
</template>

<script setup>
  import { Search, Select, SemiSelect, Open } from '@element-plus/icons-vue'
  import {
    getDispListPageApi,
    getDispByHpkApi,
    fanDispatchJungongApi,
  } from '@/api/mes/dispatch_api'
  import { localStorageConfig } from '@/config/localstorage.config'
  import queryDispDialog from './modules/queryDispDialog.vue'

  const $baseLoading = inject('$baseLoading')
  const $baseMessage = inject('$baseMessage')

  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)

  const queryForm = ref({ pkCorp: pkCorp })

  const dispH = ref()
  const disps = ref()
  const currentDispsRef = ref()

  const queryDispDialogRef = ref()

  const paginationParams = reactive({
    pageNum: 1,
    pageSize: 10,
    total: 0,
  })

  const queryDispH = async () => {
    const load = $baseLoading()
    try {
      let params = {
        ...queryForm.value,
        ...paginationParams,
      }
      const { code, msg, data } = await getDispListPageApi(params)
      if (code === 2000) {
        $baseMessage(msg, 'success')
        dispH.value = data.list
        paginationParams.total = data.total
      } else {
        $baseMessage(msg, 'error')
      }
    } finally {
      load.close()
    }
  }

  const openQueryDialog = () => queryDispDialogRef.value.openDialog()

  const queryCallback = (value) => {
    queryForm.value = value
    queryDispH()
  }

  const handleCurrentChange = async (row) => {
    const load = $baseLoading()
    try {
      let params = {
        pkDispatchH: row.pkDispatchH,
      }
      const { code, data } = await getDispByHpkApi(params)
      if (code == 2000) {
        disps.value = data
      }
    } finally {
      load.close()
    }
  }

  const handleCurrentChange2 = async (row) => {
    currentDispsRef.value = row
  }

  const fanJungong = async () => {
    const load = $baseLoading()
    try {
      if (currentDispsRef.value.state != 1) {
        $baseMessage('单据不是竣工状态，不需要反竣工。', 'warning')
      }
      let params = {
        pkDispatchSeq: currentDispsRef.value.pkDispatchSeq,
      }
      const { code, msg } = await fanDispatchJungongApi(params)
      if (code === 2000) {
        $baseMessage(msg, 'success')
        // queryDispH()
      } else {
        $baseMessage(msg, 'error')
      }
    } finally {
      load.close()
    }
  }

  //#region pagintion 开始
  const layout = ref('prev, pager, next')
  const handlePaginationCurrentChange = (...args) => {
    paginationParams.pageNum = args[0] ? args[0] : 1
  }
  const handlePaginationSizeChange = (...args) => {
    paginationParams.pageSize = args[0] ? args[0] : 20
    paginationParams.pageNum = 1
  }
  //#endregion

  watch(paginationParams, () => queryDispH())

  onMounted(() => queryDispH())
</script>
